﻿#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using System.Drawing;
using AbstractClass.NPerf.Framework;

namespace AbstractClass.NPerf
{
    internal class Program
    {
        [MTAThread]
        public static void Main(string[] args)
        {
            var testRunner = new NPerfTestRunner();
            var writer = new ResultTracer();

            TestFixture fixture = TestLoader.LoadType(typeof (DemoTest));
            writer.Attach(testRunner, true);
            testRunner.RunTestFixture(fixture, false);

            testRunner.QueryCompletedTests();

            Console.ReadLine();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    [PerfTestFixture]
    public class DemoTest
    {
        private int _count;

        ///<summary>
        ///</summary>
        [PerfTestFixtureSetUp]
        public void FixSetup()
        {
            Console.WriteLine(@"Fixture Setup");
            _count++;
        }

        ///<summary>
        ///</summary>
        [PerfTestFixtureTearDown]
        public void FixTearDown()
        {
            Console.WriteLine(@"Fixture Teardown");
            _count--;
        }

        ///<summary>
        ///</summary>
        [PerfTestSetUp]
        public void Setup()
        {
            Console.WriteLine(@"Setup");
            _count++;
        }

        ///<summary>
        ///</summary>
        [PerfTestTearDown]
        public void TearDown()
        {
            Console.WriteLine(@"TearDown");
            _count--;
        }

        ///<summary>
        ///</summary>
        [PerfTest]
        public void Method1()
        {
            Console.WriteLine(@"Method1 " + _count);
        }

        /// <summary>
        /// Method2s this instance.
        /// </summary>
        [PerfTest]
        public void Method2()
        {
            Console.WriteLine(@"Method2 " + _count);
        }

        /// <summary>
        /// Tests the method.
        /// </summary>
        [PerfTestIgnore("Ignored")]
        [PerfTest(TestType.HitCount, TestType.Duration, Threads = 1, TestIterations = 1)]
        public void TestMethod()
        {
            var source1 = (Bitmap) Image.FromFile(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg");
            //var source1 = (Bitmap)Image.FromFile(@"C:\Documents and Settings\256441\My Documents\My Pictures\printer.jpg");
            var bm = new Bitmap(source1.Width, source1.Height);

            for (int i = 0; i < 1; i++)
            {
                for (int y = 0; y < bm.Height; y++)
                {
                    for (int x = 0; x < bm.Width; x++)
                    {
                        Color c = source1.GetPixel(x, y);
                        var luma = (int) (c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
                        bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
                    }
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void ConsoleTest()
        {
            int i = 0;
            while (i < 10)
            {
                Console.WriteLine(i++);
            }
        }


        /// <summary>
        /// Asserts the test.
        /// </summary>
        [PerfTest]
        public void AssertTest()
        {
            Assert.That(The.MemoryUsageOf(TestMethod), Is.GreaterThan(6));

            Assert.That(The.CPUUsageOf(TestMethod), Is.GreaterThan(46));

            Assert.That(The.HitCountOf(TestMethod), Is.InBetween(10, 30));

            Assert.That(The.DurationOf(TestMethod), Is.GreaterThan(2000));

            Assert.That(The.DurationOf(TestMethod), Is.GreaterThan(The.DurationOf(ConsoleTest)));
        }
    }
}